package com.qiangesoft.blog.file.core;

import com.qiangesoft.blog.file.enums.FileStoreTypeEnum;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 文件存储策略上下文
 *
 * @author qiange
 * @date 2023-04-21
 * @Description : 利用Spring的发现机制,将实现了FileStoreStrategy的类都put到strategyMap里面，
 * 后面只需要对应好各个实现类的注解 如： @Component("LOCAL") 就可以取出不同的业务实现类
 */

@Service
public class FileStoreStrategyContext {

    private final Map<String, FileStoreStrategy> strategyMap = new ConcurrentHashMap<>();

    public FileStoreStrategyContext(Map<String, FileStoreStrategy> strategyMap) {
        this.strategyMap.clear();
        strategyMap.forEach((k, v) -> this.strategyMap.put(k, v));
    }

    /**
     * 获取具体的策略实现类
     *
     * @param fileStoreType
     * @return
     */
    public FileStoreStrategy getStrategy(FileStoreTypeEnum fileStoreType) {
        return strategyMap.get(fileStoreType.name());
    }
}